//
//  AppDelegate.swift
//  U17_Test
//
//  Created by dingjunchuxing on 2019/9/24.
//  Copyright © 2019 text. All rights reserved.
//

import UIKit
import Alamofire
import IQKeyboardManagerSwift


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    //网络监控
    lazy var reachability: NetworkReachabilityManager? = {
        return NetworkReachabilityManager(host: "http://app.u17.com")
    }()
    
    var orientation: UIInterfaceOrientationMask = .portrait

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
       
        
        return true
    }
   

    
    func configBase(){
        // MARK: =========== 键盘处理 =============
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        
        // MARK: =========== 性能缓存 =============
        let defaults = UserDefaults.standard
        if (defaults.value(forKey: String.sexTypeKey) == nil) {
            defaults.set(1, forKey: String.sexTypeKey)
            defaults.synchronize()
        }
        
         // MARK: =========== 网络监控 =============
        reachability?.listener = { statue in
            switch statue {
            case .reachable(.wwan): break
                //提示
            default:break
                
            }
        }
        reachability?.startListening()
        
    }
    
    
    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
      
        
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
       
    }
   
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
              return orientation

          }
}

extension UIApplication {
     
    class func changeOrientationTo(landscapeRight: Bool){
        
        guard let delegate = UIApplication.shared.delegate as? AppDelegate else { return }
        if landscapeRight == true {
            delegate.orientation = .landscapeRight
            UIApplication.shared.supportedInterfaceOrientations(for: delegate.window)
                UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
            } else {
                delegate.orientation = .portrait
                UIApplication.shared.supportedInterfaceOrientations(for: delegate.window)
                UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
        }
    }
}

